package cn.gailvlun.gll.presentation.apartment;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityApartmentManageBinding;
import cn.gailvlun.gll.databinding.TabApartmentManageBinding;
import cn.gailvlun.gll.mgr.PermissionMgr;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.presentation.apartment.adapter.ApartmentManagePagerAdapter;
import cn.gailvlun.gll.presentation.apartment.room.CreateRuleActivity;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;



public class ApartmentManageActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, ApartmentManageActivity.class);
        context.startActivity(intent);
    }

    public static void openFromNotification(Context context) {
        Intent intent = new Intent(context, ApartmentManageActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private ActivityApartmentManageBinding mBinding;

    private boolean isEnableAddRoom = PermissionMgr.isEnableAddRoom();

    private ApartmentManagePagerAdapter pagerAdapter;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        
        if (mBinding != null) {
            mBinding.vp.setCurrentItem(0);
            if (pagerAdapter.getCurrentFragment() != null) {

                pagerAdapter.getCurrentFragment().autoRefresh();
            }
        }
    }

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_apartment_manage);
        mBinding.setPresenter(new Presenter());

        Glide.with(mContext)
                .load(R.drawable.bg_emotion_cover2)
                .into(mBinding.ivBg);

        if (isEnableAddRoom) {
            mBinding.stl.setVisibility(View.VISIBLE);
        } else {
            mBinding.llAttend.setVisibility(View.VISIBLE);
        }

        final String[] tabTitles = getResources().getStringArray(R.array.apartment_manage_tab);

        pagerAdapter = new ApartmentManagePagerAdapter(getSupportFragmentManager(), isEnableAddRoom);
        mBinding.vp.setAdapter(pagerAdapter);

        mBinding.stl.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                TabApartmentManageBinding tabBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.tab_apartment_manage, container, false);
                tabBinding.tvTitle.setText(tabTitles[position]);
                switch (position) {
                    case 0: {
                        tabBinding.tvTitle.setTextColor(getResources().getColor(R.color.silverTwo));
                        break;
                    }
                    case 1: {
                        tabBinding.tvTitle.setTextColor(getResources().getColor(R.color.steelGrey));
                        break;
                    }
                }
                return tabBinding.getRoot();
            }
        });
        mBinding.stl.setViewPager(mBinding.vp);

        mBinding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < tabTitles.length; i++) {
                    View view = mBinding.stl.getTabAt(i);
                    Log.e("tag", "i: " + i);
                    TextView tv = view.findViewById(R.id.tv_title);
                    if (i == position) {
                        tv.setTextColor(getResources().getColor(R.color.silverTwo));
                    } else {
                        tv.setTextColor(getResources().getColor(R.color.steelGrey));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        mBinding.setItem(ProfileUtil.getProfile());
    }

    public class Presenter {
        public void onCreateRoom(Profile item) {
            if (isEnableAddRoom) {
                CreateRuleActivity.openActivity(mContext);
            } else {
                ToastUtil.showShort("没有权限");
            }
        }
    }
}
